//
//  vdTexNode.m
//  Void Dead
//
//  Created by Sidney Just on 17.11.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdTexNode.h"
#import "vdMacros.h"

@implementation vdTexNode
@synthesize texture, dimension_x, dimension_y, name;

- (void)loadTexture {
	CGImageRef textureImage = [UIImage imageNamed:name].CGImage;
	
	NSInteger texWidth = CGImageGetWidth(textureImage);     
    NSInteger texHeight = CGImageGetHeight(textureImage);
	
	dimension_x = texWidth;
	dimension_y = texHeight;
	
	if(!((texWidth != 0) && !(texWidth & (texWidth - 1))))
	{
		if(!((texHeight != 0) && !(texHeight & (texHeight - 1))))
		{
			vdLog(@"Trying to load non pot texture %@", name);
		}
	}
	
	GLubyte *textureData = (GLubyte *)calloc(1, texWidth * texHeight * 4);
	
	CGContextRef textureContext = CGBitmapContextCreate(textureData,
														texWidth,
														texHeight,
														8, texWidth * 4,
														CGImageGetColorSpace(textureImage),
														kCGImageAlphaPremultipliedLast);
	
	CGContextDrawImage(textureContext,                                         
					   CGRectMake(0.0, 0.0, (float)texWidth, (float)texHeight),
					   textureImage);
	
	
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, texWidth, texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData);
	
	free(textureData);
	CFRelease(textureContext);
	
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glEnable(GL_TEXTURE_2D);    
}

- (GLuint *)getTexture {
	return &texture;
}

+ (BOOL)isPowerOfTwo:(NSString *)image {
	CGImageRef textureImage = [UIImage imageNamed:image].CGImage;
	
	NSInteger texWidth = CGImageGetWidth(textureImage);     
	NSInteger texHeight = CGImageGetHeight(textureImage);
	
	if(!((texWidth != 0) && !(texWidth & (texWidth - 1))))
	{
		if(!((texHeight != 0) && !(texHeight & (texHeight - 1))))
		{
			return NO;
		}
	}
	
	return YES;
}

- (id)initWithHandle:(GLuint)handle andSize:(CGRect)rect {
	if(self = [super init])
	{
		name = NULL;
		
		texture = handle;
		dimension_x = rect.size.width - rect.origin.x;
		dimension_y = rect.size.width - rect.origin.y;
		
		vdLog(@"Created texture %@ with <%fx%f>", self, dimension_x, dimension_y);
	}
	return self;
}

- (id)init {
	if(self = [super init])
	{
		name = NULL;
	}
	return self;
}

- (id)initWithFile:(NSString *)file {
	if(self = [super init])
	{
		name = [[NSString alloc] initWithString:file];
		[self loadTexture];
	}
	return self;
}

- (void)dealloc {
	if(name)
	{
		vdLog(@"Destroyed texture %@ containing <%@>", self, name);
		[name release];
		
		glDeleteTextures(1, &texture);
	}

	
	[super dealloc];
}

@end
